package com.xb.p42_multiactiondemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;

public class MainActivity extends AppCompatActivity {

    int pointID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    //多点触摸
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //获得当前的触摸点
        pointID = event.getPointerId(0);
        //通过触摸点索引获取坐标
        int pointIndex=event.findPointerIndex(pointID);

        float x=event.getX(pointIndex);
        float y=event.getY(pointIndex);

        Log.i("--MultiAction--","触摸事件为："+actionToString(event.getAction()));
        //判断是单点还是多点
        if(event.getPointerCount()>1){
            for(int i=0;i<event.getPointerCount();i++){
                pointIndex=event.findPointerIndex(i);
                x=event.getX(pointIndex);
                y=event.getY(pointIndex);
                Log.i("--MultiAction--","多个点，第"+i+"个点，("+x+","+y+")");
            }
        }else{
            Log.i("--MultiAction--","单个点:("+x+","+y+")");
        }
        return super.onTouchEvent(event);
    }

    public static String actionToString(int action){
        switch(action){
            case MotionEvent.ACTION_DOWN:return "Down";
            case MotionEvent.ACTION_MOVE:return "Move";
            case MotionEvent.ACTION_CANCEL:return "Cancel";
            case MotionEvent.ACTION_UP: return "Up";
            case MotionEvent.ACTION_POINTER_UP:return "Pointer Up";
            case MotionEvent.ACTION_OUTSIDE:return "Outside";
            case MotionEvent.ACTION_POINTER_DOWN:return "Pointer Down";
        }
        return "";
    }
}